/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#ifndef PLEXCEPTION_LIST_H
#define PLEXCEPTION_LIST_H

/**
 * The list header.
 */
typedef struct list_head {
	/**
	 * The next element.
	 */
	struct list_head *next;
	/**
	 * The previous element.
	 */
	struct list_head *prev;
} exceptionList_t;

#define LIST_NODE_ALLOC(NAME) \
	NAME = malloc(sizeof(*NAME));

/**
 * Creates the list.
 *
 * @param list The list to init.
 */
static inline void initListHead(exceptionList_t *list) {
	list->next = list;
	list->prev = list;
}

/**
 * Insert a new entry between two known consecutive entries.
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 *
 * @param _new The new list element.
 * @param prev The previous element.
 * @param next The next element.
 */
static inline void __list_add(exceptionList_t *_new, exceptionList_t *prev, exceptionList_t *next) {
	next->prev = _new;
	_new->next = next;
	_new->prev = prev;
	prev->next = _new;
}

/**
 * Add a new entry.
 * Insert a new entry after the specified head.
 * This is good for implementing stacks.
 *
 * @param _new  new entry to be added
 * @param head list head to add it after
 */
static inline void list_add(exceptionList_t *_new, exceptionList_t *head) {
	__list_add(_new, head, head->next);
}

/**
 * Add a new entry.
 * Insert a new entry before the specified head.
 * This is useful for implementing queues.
 *
 * @param _new  new entry to be added
 * @param head list head to add it before
 */
static inline void list_add_tail(exceptionList_t *_new, exceptionList_t *head) {
	__list_add(_new, head->prev, head);
}

/**
 * Delete a list entry by making the prev/next entries
 * point to each other.
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 *
 * @param prev The previous element.
 * @param next The next element.
 */
static inline void __list_del(exceptionList_t * prev, exceptionList_t * next) {
	next->prev = prev;
	prev->next = next;
}

/**
 * Deletes entry from list.
 * 
 * @param entry the element to delete from the list
 * @note list_empty on entry does not return true after this, the entry is
 * in an undefined state.
 */
static inline void list_del(exceptionList_t *entry) {
	__list_del(entry->prev, entry->next);
#if defined __cplusplus
	entry->next = 0;
	entry->prev = 0;
#else
	entry->next = (void *)0;
	entry->prev = (void *)0;
#endif
}

/**
 * Deletes entry from list and reinitialize it.
 *
 * @param entry the element to delete from the list
 */
static inline void list_del_init(exceptionList_t *entry) {
	__list_del(entry->prev, entry->next);
	initListHead(entry);
}

/**
 * Delete from one list and add as another's head.
 *
 * @param list the entry to move.
 * @param head the head that will precede our entry.
 */
static inline void list_move(exceptionList_t *list, exceptionList_t *head) {
	__list_del(list->prev, list->next);
	list_add(list, head);
}

/**
 * Delete from one list and add as another's tail.
 *
 * @param list the entry to move.
 * @param head the head that will follow our entry.
 */
static inline void list_move_tail(exceptionList_t *list, exceptionList_t *head) {
	__list_del(list->prev, list->next);
	list_add_tail(list, head);
}

/**
 * Tests whether a list is empty.
 *
 * @param head the list to test.
 */
static inline int list_empty(const exceptionList_t *head) {
	return head->next == head;
}

static inline void __list_splice(exceptionList_t *list, exceptionList_t *head) {
	exceptionList_t *first = list->next;
	exceptionList_t *last = list->prev;
	exceptionList_t *at = head->next;

	first->prev = head;
	head->next = first;
	last->next = at;
	at->prev = last;
}

/**
 * Join two lists.
 * 
 * @param list the new list to add.
 * @param head the place to add it in the first list.
 */
static inline void list_splice(exceptionList_t *list, exceptionList_t *head) {
	if (!list_empty(list)) {
		__list_splice(list, head);
	}
}

/**
 * Join two lists and reinitialise the emptied list.
 *
 * @param list the new list to add.
 * @param head the place to add it in the first list.
 */
static inline void list_splice_init(exceptionList_t *list, exceptionList_t *head) {
	if (!list_empty(list)) {
		__list_splice(list, head);
		initListHead(list);
	}
}

/**
 * Get container of list head.
 */
#define container_of(ptr, type, member) \
	((type *)((char *)(ptr) - offsetof(type, member)))

/**
 * Get the struct for this entry.
 *
 * @param ptr    the &list_t pointer
 * @param type   the type of the struct this is embedded in
 * @param member the name of the list_struct within the struct
 */
#define list_entry(ptr, type, member) \
	container_of(ptr, type, member)

/**
 * Iterate over a list.
 *
 * @param pos  the &list_t to use as a loop counter
 * @param head the head for your list
 */
#define list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)

/**
 * Iterate over a list backwards.
 *
 * @param pos  the &list_t to use as a loop counter
 * @param head the head for your list
 */
#define list_for_each_prev(pos, head) \
	for (pos = (head)->prev; pos != (head); pos = pos->prev)

/**
 * Iterate over a list safe against removal of list entry.
 *
 * @param pos   the &list_t to use as a loop counter.
 * @param n     another &list_t to use as temporary storage.
 * @param head  the head for your list.
 */
#define list_for_each_safe(pos, n, head) \
	for (pos = (head)->next, n = pos->next; pos != (head); pos = n, n = pos->next)

/**
 * Iterate over list of given type.
 *
 * @param pos    the type * to use as a loop counter.
 * @param head:  the head for your list.
 * @param member the name of the list_struct within the struct.
 */
#define list_for_each_entry(pos, head, member) \
	for (pos = list_entry((head)->next, typeof(*pos), member); &pos->member != (head); pos = list_entry(pos->member.next, typeof(*pos), member))

/**
 * Iterate backwards over list of given type.
 *
 * @param pos    the type * to use as a loop counter.
 * @param head   the head for your list.
 * @param member the name of the list_struct within the struct.
 */
#define list_for_each_entry_reverse(pos, head, member) \
	for (pos = list_entry((head)->prev, typeof(*pos), member); &pos->member != (head); pos = list_entry(pos->member.prev, typeof(*pos), member))

/**
 * Iterate over list of given type safe against removal of list entry.
 *
 * @param pos    the type * to use as a loop counter
 * @param n      another type * to use as temporary storage
 * @param head   the head for your list
 * @param member the name of the list_struct within the struct
 */
#define list_for_each_entry_safe(pos, n, head, member) \
	for (pos = list_entry((head)->next, typeof(*pos), member), n = list_entry(pos->member.next, typeof(*pos), member); \
		&pos->member != (head); pos = n, n = list_entry(n->member.next, typeof(*n), member))

/**
 * Iterate backwards over list of given type safe against removal of list entry.
 *
 * @param pos    the type * to use as a loop counter
 * @param n      another type * to use as temporary storage
 * @param head   the head for your list
 * @param member the name of the list_struct within the struct
 */
#define list_for_each_entry_safe_reverse(pos, n, head, member) \
	for (pos = list_entry((head)->prev, typeof(*pos), member), n = list_entry(pos->member.prev, typeof(*pos), member); \
		&pos->member != (head); pos = n, n = list_entry(n->member.prev, typeof(*n), member))

#endif
